
package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.zzyl.dto.RoomDto;
import com.zzyl.entity.Room;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.service.BedService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private BedService bedService;




    /**
     * 根据楼层 id 获取房间视图对象列表
     * @param floorId 楼层 id
     * @return 房间视图对象列表
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
       return roomMapper.selectByFloorId(floorId);
    }

    @Override
    public Map<String, Integer> countRoomByTypeName(List<String> collect) {
        if (CollUtil.isEmpty(collect)) {
            return new HashMap<>();
        }
        return roomMapper.countRoomByTypeName(collect);
    }

    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 获取所有房间（床位房型）
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsCheckedByFloorId(Long floorId) {
        return roomMapper.selectRoomsCheckedByFloorId(floorId);
    }


    /**
     * 添加房间
     * @param roomDto
     */
    @Override
    public void add(RoomDto roomDto) {
        Room room = BeanUtil.toBean(roomDto, Room.class);
        room.setCreateTime(LocalDateTime.now());
        room.setUpdateTime(LocalDateTime.now());
        room.setCreateBy(1L);
        room.setUpdateBy(1L);
        roomMapper.add(room);
    }

    /**
     * 根据id删除房间
     * @param id
     */
    @Override
    public void delete(Integer id) {
        roomMapper.delete(id);
    }

    /**
     * 查询回显
     * @param id
     * @return
     */
    @Override
    public RoomVo getRoomById(Integer id) {
        RoomVo roomVo=roomMapper.getRoomById(id);
        return roomVo;
    }

    @Override
    public void update(RoomDto roomDto) {
        Room room = BeanUtil.toBean(roomDto, Room.class);
        room.setUpdateTime(LocalDateTime.now());
        room.setUpdateBy(1L);
        roomMapper.update(room);
    }

    @Override
    public List<RoomVo> getAllRooms() {
        List <RoomVo> roomVos =roomMapper.getAllRooms();
        return roomVos;
    }

}

